﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsVoteItem
    {
        public int Add(KeRui.Model.CmsVoteItem model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.Int, 4), new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 300), new SqlParameter("@VoteNum", SqlDbType.Int, 4), new SqlParameter("@CreateTime", SqlDbType.DateTime) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.VoteID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.VoteNum;
            parameters[4].Value = model.CreateTime;
            DbHelperSQL.RunProcedure("CmsVoteItem_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int VoteItemID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteItemID;
            DbHelperSQL.RunProcedure("CmsVoteItem_Delete", parameters, out num);
        }

        public bool Exists(int VoteItemID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteItemID;
            return (DbHelperSQL.RunProcedure("CmsVoteItem_Exists", parameters, out num) == 1);
        }

        public int GetItemCount(int VoteID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            return DbHelperSQL.RunProcedure("CmsVoteItem_Sum", parameters, out num);
        }

        public KeRui.Model.CmsVoteItem GetModel(int VoteItemID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteItemID;
            KeRui.Model.CmsVoteItem item = new KeRui.Model.CmsVoteItem();
            DataSet set = DbHelperSQL.RunProcedure("CmsVoteItem_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["VoteItemID"].ToString() != "")
                {
                    item.VoteItemID = int.Parse(set.Tables[0].Rows[0]["VoteItemID"].ToString());
                }
                if (set.Tables[0].Rows[0]["VoteID"].ToString() != "")
                {
                    item.VoteID = int.Parse(set.Tables[0].Rows[0]["VoteID"].ToString());
                }
                item.Title = set.Tables[0].Rows[0]["Title"].ToString();
                if (set.Tables[0].Rows[0]["VoteNum"].ToString() != "")
                {
                    item.VoteNum = int.Parse(set.Tables[0].Rows[0]["VoteNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    item.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return item;
            }
            return null;
        }

        public void Update(KeRui.Model.CmsVoteItem model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 300), new SqlParameter("@VoteNum", SqlDbType.Int, 4) };
            parameters[0].Value = model.VoteItemID;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.VoteNum;
            DbHelperSQL.RunProcedure("CmsVoteItem_Update", parameters, out num);
        }

        public int UpdateNum(string VoteItemID, KeRui.Model.CmsVoteIp model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteItemID", SqlDbType.VarChar, 200), new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.NVarChar, 50), new SqlParameter("@IpAddress", SqlDbType.VarChar, 20) };
            parameters[0].Value = VoteItemID;
            parameters[1].Value = model.VoteID;
            parameters[2].Value = model.UserID;
            parameters[3].Value = model.CreateIP;
            return DbHelperSQL.RunProcedure("CmsVoteItem_NumUpdate", parameters, out num);
        }
    }
}

